package com.zhb.zhbojbackendgateway.filter;

import org.springframework.http.server.reactive.ServerHttpRequest;

public class NetUtils {

    public static String getIpAddress(ServerHttpRequest request) {
        String ipAddress = request.getRemoteAddress() != null ? request.getRemoteAddress().getAddress().getHostAddress() : null;
        // 如果请求经过了代理服务器，可以从 X-Forwarded-For 头获取真实的 IP 地址
        if (ipAddress == null) {
            ipAddress = request.getHeaders().getFirst("X-Forwarded-For");
        }
        return ipAddress != null ? ipAddress.split(",")[0] : "Unknown";
    }
}
